package com.hehao.generator;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.sql.Types;
import java.util.Collections;
import java.util.List;


/**
 * @author User-SJD0004
 */
public class FastCodeGenerator {
    public static void main(String[] args) {
        String url = "jdbc:mysql://127.0.0.1:3306/damon?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai";
        String username = "root";
        String password = "root";
        FastAutoGenerator.create(url, username, password)
                .globalConfig(builder -> {
                    builder.author("Damon") // 设置作者
                            .enableSpringdoc() //开启 springdoc 模式
                            .outputDir("D:\\develop\\workspace\\Damon_Study\\Generator\\src\\main\\java"); // 指定输出目录
                })
                .dataSourceConfig(builder -> builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
                    int typeCode = metaInfo.getJdbcType().TYPE_CODE;
                    if (typeCode == Types.SMALLINT) {
                        // 自定义类型转换
                        return DbColumnType.INTEGER;
                    }
                    return typeRegistry.getColumnType(metaInfo);

                }))
                .packageConfig(builder -> {
                    builder.parent("com.hehao.generator") // 设置父包名
                            .pathInfo(Collections.singletonMap(OutputFile.controller, "D:\\develop\\workspace\\Damon_Study\\Generator\\src\\main\\java\\com\\hehao\\generator\\files")) // 设置controller生成路径
                            .pathInfo(Collections.singletonMap(OutputFile.service, "D:\\develop\\workspace\\Damon_Study\\Generator\\src\\main\\java\\com\\hehao\\generator\\files")) // 设置service生成路径
                            .pathInfo(Collections.singletonMap(OutputFile.serviceImpl, "D:\\develop\\workspace\\Damon_Study\\Generator\\src\\main\\java\\com\\hehao\\generator\\files")) // 设置serviceImpl生成路径
                            .pathInfo(Collections.singletonMap(OutputFile.mapper, "D:\\develop\\workspace\\Damon_Study\\Generator\\src\\main\\java\\com\\hehao\\generator\\files")) // 设置mapper生成路径
                            .pathInfo(Collections.singletonMap(OutputFile.entity, "D:\\develop\\workspace\\Damon_Study\\Generator\\src\\main\\java\\com\\hehao\\generator\\files")) // 设置entity生成路径
                            .pathInfo(Collections.singletonMap(OutputFile.xml, "D:\\develop\\workspace\\Damon_Study\\Generator\\src\\main\\java\\com\\hehao\\generator\\files")); // 设置mapperXml生成路径
                })
                .strategyConfig(builder -> {
                    builder.addInclude(List.of("medsci_action_detail")) // 设置需要生成的表名
                            .serviceBuilder().enableFileOverride()
                            .entityBuilder().enableLombok().enableFileOverride()
                            .controllerBuilder().enableRestStyle().enableFileOverride()
                            .mapperBuilder().enableBaseResultMap().enableBaseColumnList().enableFileOverride();
                })
//                .templateConfig(consumer -> {
//                    consumer.controller("template/controller.java")
//                            .service("template/service.java")
//                            .serviceImpl("template/serviceImpl.java")
//                            .entity("template/entity.java")
//                            .mapper("template/mapper.java")
//                            .build();
//                })
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }
 
}
